<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/5
 * Time: 15:35
 */

namespace App\Game\Traits;

use Carbon\Carbon;

trait SgTrait
{
    /**
     * gameapi
     * 2018/9/21 9:43
     * Administrator
     * parseTimeInterval
     * 将时间拆分成最长10分钟的时间段
     * @param Carbon $startTime
     * @param Carbon $endTime
     * @return array
     */
    public function parseTimeInterval(Carbon $startTime, Carbon $endTime)
    {
        $timeArray = [];
        $startMinute = (clone $startTime)->startOfMinute();
        $diffMinutes = $startMinute->diffInMinutes($endTime, false);
        $times = (int)ceil($diffMinutes / 10);

        if ($startTime->diffInMinutes($endTime, false) >= 10) {
            for ($i = 0; $i < $times; $i++) {
                if ($i < $times - 1) {
                    $start = (clone $startTime)->addMinute($i * 10)->format('Y-m-d H.i.s');
                    $end = (clone $startTime)->addMinute(($i + 1) * 10)->format('Y-m-d H.i.s');
                } else {
                    $start = (clone $startTime)->addMinute($i * 10)->format('Y-m-d H.i.s');
                    $end = (clone $endTime)->format('Y-m-d H.i.s');
                }

                $timeArray[] = compact('start', 'end');
            }
        } else {
            $start = (clone $startTime)->format('Y-m-d H.i.s');
            $end = (clone $endTime)->format('Y-m-d H.i.s');
            $timeArray[] = compact('start', 'end');
        }

        return $timeArray;
    }

    /**
     * gameapi
     * 2018/9/24 17:36
     * Administrator
     * statusParse
     * 解析状态字段
     * @param array $apiData
     * @param array $data
     * @return array
     */

    private function sgRequest($method = 'POST', $url, $param = [])
    {
        //hash
        if (isset($param['username'])){
            $param['username'] = strtolower($param['username']);
        }
        $param['root'] = $this->merchantCode;
        ksort($param,SORT_NATURAL);
        $str = '';
        foreach ($param as $k => $v){
            $str .= $k.'='.$v.'&';
        }
        $str .= $this->apiKey;
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'sgsgsg.111..'.$str.PHP_EOL, FILE_APPEND);
        $param['hash'] = md5($str);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'sgsgsg...'.json_encode($param).PHP_EOL, FILE_APPEND);
        $output = $this->request($method, $url, $param);
        return $output;
    }
}